package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicPositionmaterial;
import cn.stylefeng.guns.modular.basic.mapper.BasicPositionmaterialMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionmaterialParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionmaterialResult;
import  cn.stylefeng.guns.modular.basic.service.BasicPositionmaterialService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 库位物料表 服务实现类
 */
@Service
public class BasicPositionmaterialServiceImpl extends ServiceImpl<BasicPositionmaterialMapper, BasicPositionmaterial> implements BasicPositionmaterialService {

    @Override
    public void add(BasicPositionmaterialParam param){
        BasicPositionmaterial entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicPositionmaterialParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicPositionmaterialParam param){
        BasicPositionmaterial oldEntity = getOldEntity(param);
        BasicPositionmaterial newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicPositionmaterialResult findBySpec(BasicPositionmaterialParam param,Long operateId){
        return this.baseMapper.findBySpec(param,operateId);
    }

    @Override
    public List<BasicPositionmaterialResult> findListBySpec(BasicPositionmaterialParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicPositionmaterialParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicPositionmaterialParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicPositionmaterial getOldEntity(BasicPositionmaterialParam param) {
        return this.getById(getKey(param));
    }

    private BasicPositionmaterial getEntity(BasicPositionmaterialParam param) {
        BasicPositionmaterial entity = new BasicPositionmaterial();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
